<?php
class Group extends AppModel {
	
	var $name = 'Group';

	var $actsAs = array(
		'Acl' => array(
			'requester'
		),
		'MultipleValidatable'
	);

	/**
	 * ============================================== VALIDATIONS ==============================================
	 */
	/**
	 * Default validation ruleset
	 */
    var $validate = array();
    
	/**
	 * Custom validation rulesets
	 */
	var $validationSets = array(
        'admin_add' => array(
    		  'title' => array(
					'titleRule1' => array(
						/**
						 * array('custom', '/^[a-zA-Z0-9][a-zA-Z0-9\-]{0,253}[a-zA-Z0-9]$/')
						 * 
						 * - Minimalna dyljina: 2 simvola
						 * - Maksimalna dyljina: 255 simvola
						 * - Pyrviq simvol trqbva da e bukva ili cifra: "^[a-zA-Z0-9]"
						 * - Posledniq simvol trqbva da e bukva ili cifra: "[a-zA-Z0-9]$"
						 * - Srednite simvoli ako ima takiva trqbva da sydyrjat bukvi i/ili cifri i/ili tireta. Min dyljina: 0. Max dyljina: 253.
						 */
				        'rule' => array('custom', '/^[a-zA-Z0-9][a-zA-Z0-9\-]{0,253}[a-zA-Z0-9]$/'),
						'required' => true,
						'last' => true
				    ),
		    		'titleRule2' => array(
				    	/**
				    	 * 'rule' => 'isUnique'
				    	 * 
				    	 * Stoinosta trqbva da e unikalna v tablicata na BD
				    	 */
			    		'rule' => 'isUnique',
						'last' => true
					)
			  ),
			  'parent_id' => array(
					'parent_idRule1' => array(
						/**
						 * array('comparison', '>=', 0)
						 * 
						 * - Ne e zadyljitelno za popylvane - "'required' => true" go nqma
						 * - Minimalna dyljina: 1 simvol
						 * - Samo polojitelni chisla ili nula
						 */
				        'rule' => array('comparison', '>=', 0),
						'last' => true
				    )
			  )
    	),
    	'admin_edit' => array(
    		  'title' => array(
					'titleRule1' => array(
						/**
						 * array('custom', '/^[a-zA-Z0-9][a-zA-Z0-9\-]{0,253}[a-zA-Z0-9]$/')
						 * 
						 * - Minimalna dyljina: 2 simvola
						 * - Maksimalna dyljina: 255 simvola
						 * - Pyrviq simvol trqbva da e bukva ili cifra: "^[a-zA-Z0-9]"
						 * - Posledniq simvol trqbva da e bukva ili cifra: "[a-zA-Z0-9]$"
						 * - Srednite simvoli ako ima takiva trqbva da sydyrjat bukvi i/ili cifri i/ili tireta. Min dyljina: 0. Max dyljina: 253.
						 */
				        'rule' => array('custom', '/^[a-zA-Z0-9][a-zA-Z0-9\-]{0,253}[a-zA-Z0-9]$/'),
						'required' => true,
						'last' => true
				    ),
		    		'titleRule2' => array(
				    	/**
				    	 * 'rule' => 'isUnique'
				    	 * 
				    	 * Stoinosta trqbva da e unikalna v tablicata na BD
				    	 */
			    		'rule' => 'isUnique',
						'last' => true
					)
			  ),
			  'parent_id' => array(
					'parent_idRule1' => array(
						/**
						 * array('comparison', '>=', 0)
						 * 
						 * - Ne e zadyljitelno za popylvane - "'required' => true" go nqma
						 * - Minimalna dyljina: 1 simvol
						 * - Samo polojitelni chisla ili nula
						 */
				        'rule' => array('comparison', '>=', 0),
						'last' => true
				    )
			  )
    	)
    );
	/**
	 * =============================================/ VALIDATIONS ==============================================
	 */
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
			'User' => array('className' => 'User',
								'foreignKey' => 'group_id',
								'dependent' => true,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			)
	);

	function afterSave($created) {
		if ($created) {
			// its a creation
			$id = $this->getLastInsertID();
			$aro = new Aro();
			$aro->updateAll(    array('alias'=>'\'Group:'.$id.'\''),
			array(  'Aro.model'=>'Group',
                                        'Aro.foreign_key'=>$id)
			);
		}
		else {
			// its an edit, we have to update the tree
			$data = $this->read();
			$parent_id = $data['Group']['parent_id'];

			$aro = new Aro();

			$aro_record = $aro->findByForeignKey( $this->id );
			$parent_record = $aro->findByForeignKey( $parent_id );

			if (empty($aro_record)) {
				// orphaned child
				$this->Aro->save(array(
                    'model' => $this->name,
                    'foreign_key' => $this->id,
                    'alias' => $this->name.':'.$this->id,
    			    'parent_id'		=> $parent_record['Aro']['id']
				));
			}
			else {
				// just moving nodes
				$this->Aro->save(array(
                    'model' => $this->name,
                    'foreign_key' => $this->id,
                    'alias' => $this->name.':'.$this->id,
    			    'parent_id'		=> $parent_record['Aro']['id'],
    				'id'			=> $aro_record['Aro']['id']
				));
			}
		}

		return true;
	}

	function parentNode() {
		// This should be the alias of the parent $model::$id
		$data = $this->read();
		
		// This needs to be unique
		return 'Group:'.$data['Group']['parent_id'];
	}

	/*
	 * ot http://book.cakephp.org/view/645/Acts-As-a-Requester
	 * Sluji za `alias` v aros tablicata
	 */
	/*function parentNode() {
	 return null;
	 }*/
	
}
?>